package com.itheima.edu.info.student.manager.controller;

import com.itheima.edu.info.student.manager.domain.Teacher;
import com.itheima.edu.info.student.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    //创建业务员对象
    TeacherService service = new TeacherService();
    Scanner sc = new Scanner(System.in);

    public void start() {

        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    addTeacher();
                    break;
                case 2:
                    deleteTeacher();
                    break;
                case 3:
                    updateTeacher();
                    break;
                case 4:
                    findAllTeacher();
                    break;
                case 5:
                    System.out.println("感谢您使用老师管理系统, 再见!");
                    return;
                default:
                    System.out.println("您的输入有误, 请重新输入");
                    break;
            }
        }

    }

    //查看功能
    private void findAllTeacher() {
        Teacher[] teas = service.findAllteacher();
        if (teas == null) {
            System.out.println("查无信息,请添加后重试");
        } else {
            System.out.println("学号\t\t姓名\t年龄\t生日");
            for (int i = 0; i < teas.length; i++) {
                if (teas[i] != null) {
                    System.out.println(teas[i].getId() + "\t" + teas[i].getName() + "\t" + teas[i].getAge() + "\t\t" + teas[i].getBirthday());
                }
            }

        }
    }


    //修改功能
    private void updateTeacher() {
        Teacher[] teas = service.findAllteacher();
        if (teas == null) {
            System.out.println("查无信息,请添加后重试");
            return;
        }
        //1.提示用户输入要修改的老师id
        //2.查询该老师id是否存在,如果不存在提示重新输入
        String updateId = imputIdInfo(true);
        Teacher tea = getTeacher(updateId);
        //将封装好的新老师对象和要修改的id传给业务员,指挥业务员进行修改操作
        service.updateTeacher(updateId, tea);
        System.out.println("修改成功");

    }

    //删除功能
    private void deleteTeacher() {
        Teacher[] teas = service.findAllteacher();
        if (teas == null) {
            System.out.println("查无信息,请添加后重试");
            return;
        }
        String id = imputIdInfo(true);
        service.deleteTeacher(id);
        System.out.println("删除成功");

    }

    //添加功能
    private void addTeacher() {
        System.out.println("请输入老师学号");
        String id = imputIdInfo(false);
        Teacher tea = getTeacher(id);
        //指挥业务员对象进行添加老师的业务处理
        boolean result = service.addTeacher(tea);
        //根据业务员的添加操作处理结果输出添加成功或添加失败
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    //封装对象功能
    public Teacher getTeacher(String id) {
        System.out.println("请输入老师姓名");
        String name = sc.next();
        System.out.println("请输入老师年龄");
        String age = sc.next();
        System.out.println("请输入老师出生日期");
        String birthday = sc.next();
        Teacher tea = new Teacher();
        //将接收到的老师信息封装成老师对象
        tea.setId(id);
        tea.setName(name);
        tea.setAge(age);
        tea.setBirthday(birthday);
        return tea;
    }

    //键盘录入得到相应的id方法
    public String imputIdInfo(boolean flag) {
        String id;
        while (true) {
            System.out.println("请输入要操作的id");
            id = sc.next();
            boolean exists = service.isExists(id);
            if (exists == flag) {
                break;
            } else {
                System.out.println("当前id无法使用,请重新输入");
            }
        }
        return id;
    }
}
